package com.opensky.qatest.ui.component;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;

import com.opensky.qatest.ui.page.thirdparty.FacebookLoginPage;
import com.opensky.qatest.ui.page.thirdparty.PlaxoContactPage;
import com.opensky.qatest.ui.page.thirdparty.TwitterSignInPage;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * Invite form form on Invite page
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public class InvitePageForm implements IComponent {

    // ************************ Locators ************************

    private static final String INVITE_FORM = "//section[@class='account invite big-wrapper clearfix']";

    private static final String PERSONAL_LINK = "//section[@class='account invite big-wrapper clearfix']//input[@id='personal-link' and contains(@value, 'http://osky.co/')]";

    private static final String COPY_LINK_BTN = "//section[@class='account invite big-wrapper clearfix']//button[@id='copy-personal-link' and text()='Copy link']";

    private static final String SHARE_ON_FACEBOOK = "//section[@class='account invite big-wrapper clearfix']//span[@class='share-on-fb']/a";

    private static final String SHARE_ON_TWITTER = "//section[@class='account invite big-wrapper clearfix']//span[@class='share-on-twitter']/a";

    private static final String EMAILS_TEXTAREA = "//section[@class='account invite big-wrapper clearfix']//textarea[@id='invite_emails']";

    private static final String ADD_FROM_CONTACTS_BTN = "//section[@class='account invite big-wrapper clearfix']//div[@id='plaxo-button']//button";

    private static final String SEND_NOW_BTN = "//section[@class='account invite big-wrapper clearfix']//input[@type='submit']";

    private final WebDriverWrapper driver;

    public InvitePageForm(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************
    public FacebookLoginPage clickShareOnFacebookBtn() {

	driver.saveBeforeHandles();
	driver.waitAndClick("SHARE_ON_FACEBOOK", By.xpath(SHARE_ON_FACEBOOK));

	FacebookLoginPage loginPage = new FacebookLoginPage(driver);

	return loginPage.isPageLoaded() ? loginPage : null;
    }

    public TwitterSignInPage clickShareOnTwitterBtn() {

	driver.saveBeforeHandles();
	driver.waitAndClick("SHARE_ON_TWITTER", By.xpath(SHARE_ON_TWITTER));

	TwitterSignInPage signInPage = new TwitterSignInPage(driver);

	return signInPage.isPageLoaded() ? signInPage : null;
    }

    public void sendInvitations(List<String> toEmails) {

	String emailsText = "";
	for (String toEmail : toEmails) {
	    emailsText += toEmail + ", ";
	}
	emailsText = StringUtils.removeEnd(emailsText, ", ");

	driver.waitAndSendKeys("EMAILS_TEXTAREA", By.xpath(EMAILS_TEXTAREA), emailsText);
	driver.waitAndClick("SEND_NOW_BTN", By.xpath(SEND_NOW_BTN));
    }

    public PlaxoContactPage clickAddFromYourContactsBtn() {

	driver.saveBeforeHandles();
	driver.waitAndClick("ADD_FROM_CONTACTS_BTN", By.xpath(ADD_FROM_CONTACTS_BTN));

	PlaxoContactPage contactPage = new PlaxoContactPage(driver);

	return contactPage.isPageLoaded() ? contactPage : null;
    }

    // ************************ Validations **************************

    @Override
    public boolean isComponentLoaded() {
	return driver.waitAndVerifyElementPresent("INVITE_FORM", By.xpath(INVITE_FORM));
    }

    public boolean isBaseElementsPresent() {

	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("PERSONAL_LINK", By.xpath(PERSONAL_LINK)),
		driver.waitAndVerifyElementPresent("COPY_LINK_BTN", By.xpath(COPY_LINK_BTN)),
		driver.waitAndVerifyElementPresent("SHARE_ON_FACEBOOK", By.xpath(SHARE_ON_FACEBOOK)),
		driver.waitAndVerifyElementPresent("SHARE_ON_TWITTER", By.xpath(SHARE_ON_TWITTER)),
		driver.waitAndVerifyElementPresent("EMAILS_TEXTAREA", By.xpath(EMAILS_TEXTAREA)),
		driver.waitAndVerifyElementPresent("ADD_FROM_CONTACTS_BTN", By.xpath(ADD_FROM_CONTACTS_BTN)),
		driver.waitAndVerifyElementPresent("SEND_NOW_BTN", By.xpath(SEND_NOW_BTN)));
    }

    @Override
    public String getName() {
	return "Invite form";
    }
}
